package ro.isdc.vms.vehicleadvertisement.dao.hibernate;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ro.isdc.vms.vehicleadvertisement.dao.VehicleAdvertisementDAO;
import ro.isdc.vms.vehicleadvertisement.domain.VehicleAdvertisement;

/**
 * Vehicle Advertisement Data Access Object Implementation.
 * 
 * @author Andrei Chirila / ISDC! Romania
 * @version 1.0.0
 * @date Jul 21, 2009
 */
public class VehicleAdvertisementDAOImpl extends HibernateDaoSupport implements VehicleAdvertisementDAO {
	
  /**
   * {@inheritDoc}
   */
	public VehicleAdvertisement update(final VehicleAdvertisement vehicleAdvertisement) {
		System.out.println("Updating");
		getHibernateTemplate().update(vehicleAdvertisement);
	  getHibernateTemplate().flush();
		return vehicleAdvertisement;
	}

  /**
   * {@inheritDoc}
   */
	@SuppressWarnings("unchecked")
	public List<VehicleAdvertisement> findOldFlaggedAdvertisements() {
		return getHibernateTemplate().find(
				"select va from VehicleAdvertisement va, PartyConfiguration pc " +
				"where va.isFlaggedAsOld=1 and va.vehicleInstance.registration.registererParty.entityId = pc.party.entityId " + 
				"and adddate(va.deletionTimestamp, pc.deleteOldAdvertisementAfterDays) < CURRENT_TIMESTAMP()");
	}

  /**
   * {@inheritDoc}
   */
	public void delete(final VehicleAdvertisement vehicleAdvertisement) {
		getHibernateTemplate().delete(vehicleAdvertisement);
		getHibernateTemplate().flush();
	}

}
